package com.wy.qq_6_0_sideslip;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;

/**
 * Time:8/10/21
 * Author:ypf
 * Description:
 */
public class MySlidingMenu extends HorizontalScrollView {


    private String TAG = "MySlidingMenu";

    private float mRightPadding = dp2px(30);

    private int mMenuWidth = (int) (getScreenWidth() - mRightPadding);

    private int mContentWidth = getScreenWidth();

    // 菜单是否打开
    private boolean mMenuIsOpen = false;

    //是否拦截
    private boolean mIntercept = false;

    //菜单栏
    private View mMenuView;
    //内容栏
    private View mContentView;
    //阴影
    private View mShadowView;

    private GestureDetector mGestureDetector;

    //思路
    //step1:控制菜单栏和内容栏宽度
    //step2:切换菜单栏打开和关闭
    //step3:处理动画

    public MySlidingMenu(Context context) {
        this(context, null, 0);
    }

    public MySlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(context, new MyGestureDetector());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mIntercept = false;
        if (mMenuIsOpen) {
            float currentX = ev.getX();
            if (currentX > mMenuWidth) {
                //关闭菜单
                closeMenu();
                //拦截点击事件
                mIntercept = true;
                return true;
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        //外面linearLayout
        ViewGroup container = (ViewGroup) getChildAt(0);

        //菜单栏
        mMenuView = container.getChildAt(0);
        mContentView = container.getChildAt(1);

        //限定菜单栏和内容栏的宽度
        mMenuView.getLayoutParams().width = mMenuWidth;
        mContentView.getLayoutParams().width = mContentWidth;

//        // 把内容布局单独提取出来
//        // 内容栏
        mContentView = container.getChildAt(1);
        ViewGroup.LayoutParams contentParams = mContentView.getLayoutParams();
        container.removeView(mContentView);
        // 然后在外面套一层阴影
        RelativeLayout contentContainer = new RelativeLayout(getContext());
        contentContainer.addView(mContentView);
        mShadowView = new View(getContext());
        mShadowView.setBackgroundColor(Color.parseColor("#55000000"));
        mShadowView.setAlpha(0.0f);
        contentContainer.addView(mShadowView);

        // 最后在把容器放回到原来的位置
        contentParams.width = mContentWidth;
        contentContainer.setLayoutParams(contentParams);
        container.addView(contentContainer);
    }

    /**
     * 控制首次进来显示内容页
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        // 布局指定后会从新摆放子布局，当其摆放完毕后，让菜单滚动到不可见状态
        if (changed) {
            scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        if (mIntercept) {
            return true;
        }

        if (mGestureDetector.onTouchEvent(ev)) {
            return mGestureDetector.onTouchEvent(ev);
        }

        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                //控件离左边的距离
                int currentScrollX = getScrollX();
                Log.i(TAG, "currentScrollX:" + currentScrollX);
                if (currentScrollX > mMenuWidth / 2) {
                    //关闭
                    closeMenu();
                } else {
                    //打开
                    openMenu();
                }
                return false;
        }
        return super.onTouchEvent(ev);
    }

    private void closeMenu() {
        Log.i(TAG, "closeMenu");
        smoothScrollTo(mMenuWidth, 0);
        mMenuIsOpen = false;
    }

    private void openMenu() {
        Log.i(TAG, "openMenu");
        smoothScrollTo(0, 0);
        mMenuIsOpen = true;
    }

    private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // 当手指快速滑动时候回调的方法
            Log.i(TAG, "velocityX：" + velocityX);
            // 如果菜单打开 并且是向左快速滑动 切换菜单的状态
            if (mMenuIsOpen) {
                if (velocityX < -500) {
                    toggleMenu();
                    return true;
                }
            } else {
                // 如果菜单关闭 并且是向右快速滑动 切换菜单的状态
                if (velocityX > 500) {
                    toggleMenu();
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 切换菜单的状态
     */
    private void toggleMenu() {
        Log.i(TAG, "toggleMenu mMenuIsOpen：" + mMenuIsOpen);
        if (mMenuIsOpen) {
            closeMenu();
        } else {
            openMenu();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
//        Log.e(TAG, "l ->"+l);
        //算一个梯度值
        float scale = 1f * l / mMenuWidth;

        float alphaScale = 1 - scale;
        mShadowView.setAlpha(alphaScale);

//        //右边的缩放：最小是0.7f，最大是1f
//        float rightScale = 0.7f+0.3f*scale;
//        //设置右边的缩放，默认是以中心点缩放
//        //设置缩放的中心点位置
//        ViewCompat.setPivotX(mContentView, 0);
//        ViewCompat.setPivotY(mContentView, mContentView.getMeasuredHeight()/2);
//        ViewCompat.setScaleX(mContentView, rightScale);
//        ViewCompat.setScaleY(mContentView, rightScale);
//
//        //菜单的缩放和透明度
//        //透明度是 半透明到完全透明 0.5f - 1.0f
//        float leftAlpha = 0.5f + (1-scale) *0.5f;
//        ViewCompat.setAlpha(mMenuView, leftAlpha);
//        //缩放 0.7f - 1.0f
//        float leftScale = 0.7f + (1-scale) *0.3f;
//        ViewCompat.setScaleX(mMenuView, leftScale);
//        ViewCompat.setScaleY(mMenuView, leftScale);
//
//        //最后一个效果 退出这个按钮刚开始是在右边，安装我们目前的方式永远都是在左边
//        //设置平移，先看一个抽屉效果
//        ViewCompat.setTranslationX(mMenuView, 0.25f*l);
        //平移 1*0.7f
//        ViewCompat.setTranslationX(mMenuView, 0.25f*l);

    }

    private float dp2px(int dip) {
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
        return px;
    }

    private int getScreenWidth() {
        Resources resources = this.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

}
